/** bcmi_frame task interface
 *
 *  bcmi_frame task interface
 *
 *  @author : huicong
 *  @date : 2008-11-03
 *
 */
#include "FrameTask.h"
#include "FrameJob.h"
#include "ParallelFrame.h"

namespace bcmi_frame
{

    FrameTask::FrameTask():
               _myJob(NULL),
                   _taskName(""),
                   _beRegistered(FALSE),
                   _taskType(bcmi_frame::WORKER)
    {
    }

    FrameTask::~FrameTask()
    {
        _myJob = NULL;
    }

    VOID FrameTask::linkJob(_IN FrameJob *_job)
    {
        _myJob = _job;
        _beRegistered = TRUE;
    }

    VOID FrameTask::setTaskId(IN UINT _id)
    {
        _taskId = _id;
    }

    UINT FrameTask::getTaskId() const
    {
        return _taskId;
    }

    VOID FrameTask::setTaskName(IN STRING _name)
    {
        _taskName = _name;
    }

    STRING FrameTask::getTaskName() const
    {
        return _taskName;
    }

    TaskType FrameTask::getTaskType() const
    {
        return _taskType;
    }

    VOID FrameTask::setTaskType(IN TaskType _type)
    {
        _taskType = _type;
    }

    BOOL FrameTask::beRegistered()
    {
        return _beRegistered;
    }

    StreamReader* FrameTask::getReader(IN UINT _pid)
    {
        if (_reader.find(_pid) == _reader.end())
        {
            MPIReader *_r = new MPIReader(_pid, _myJob->getFrame());

            _reader[_pid] = _r;
        }
        return (_reader.find(_pid))->second;
    }

    StreamWriter* FrameTask::getWriter(IN UINT _pid)
    {
        if (_writer.find(_pid) == _writer.end())
        {
            MPIWriter *_w = new MPIWriter(_pid, _myJob->getFrame());

            _writer[_pid] = _w;
        }
        return (_writer.find(_pid))->second;
    }

    VOID FrameTask::insertRecvId(IN UINT _id)
    {
        _recvSet.insert(_id);
    }

    HRESULT FrameTask::frameInitialize()
    {
        HRESULT hr = H_OK;

        _writer.clear();
        _reader.clear();

        frameInitializeRecv();

        return H_OK;
    }

    HRESULT FrameTask::frameFinalize()
    {

        frameFinalizeSend();

        _recvSet.clear();// clear the set of process_id which send buffer to this 

        for (READER_MAP::iterator ite = _reader.begin();
            _reader.end() != ite;
            ++ite)
        {
            delete (*ite).second;// delete all reader
        }

        for (WRITER_MAP::iterator ite = _writer.begin();
            _writer.end() != ite;
            ++ite)
        {
            delete (*ite).second;// delete all writer
        }
        return H_OK;
    }

    HRESULT FrameTask::frameInitializeRecv()
    {
        HRESULT hr = H_OK;
        for (RECV_SET::iterator ite = _recvSet.begin();
            _recvSet.end() != ite;
            ++ite)
        {
            MPIReader *_reader = (MPIReader*)getReader((*ite));
            hr = _reader->getBufferPool();
            if (FAILED(hr))
            {
                //UNDO Log
                return hr;
            }
        }
        return hr;
    }

    HRESULT FrameTask::frameFinalizeSend()
    {
        HRESULT hr = H_OK;
        for (WRITER_MAP::iterator ite = _writer.begin();
            _writer.end() !=ite;
            ++ite)
        {
            hr = (ite->second)->putBufferPool();
            if (FAILED(hr))
            {
                //UNDO Log
                return hr;
            }
        }
        return hr;
    }


}; //namespace bcmi_frame